package com.sry.blog.sys.controller;


import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sry.blog.common.api.CommonResult;
import com.sry.blog.sys.model.User;
import com.sry.blog.sys.service.UserService;
import com.sry.blog.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * InnoDB free: 10240 kB 前端控制器
 * </p>
 *
 * @author SR_Y
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;



    /**
     * 用户修改
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public boolean update(@RequestBody User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        log.info("修改用户信息：{}",user.toString());
        return userService.creatOrUpdate(user);
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public boolean save(@RequestBody User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String password = user.getPassword();
        String salt = MD5Util.setSalt(5 + new Random().nextInt(5));
        password = MD5Util.setPassword(password, salt);
        user.setPassword(password);
        user.setSalt(salt);
        user.setCreateTime(new Date());
        if (!StrUtil.isNotEmpty(user.getNickName())) user.setNickName(UUID.fastUUID().toString(true));

        return userService.creatOrUpdate(user);
    }


    /**
     * 获取用户列表
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<User>> userList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "5")Integer pageSize) {
        return CommonResult.success(userService.list(pageNum,pageSize));
    }

    /**
     * 条件查询
     * @param user
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/lists", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Page<User>> userLists(@RequestBody User user,
                                             @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize) {

        log.info("{}",user.toString());
        return CommonResult.success(userService.lists(user,pageNum, pageSize));
    }

    /**
     * test
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public CommonResult test(@RequestParam("file") MultipartFile file) throws IOException {
        return CommonResult.success("test");
    }

    /**通过ID查找
     * @param id
     * @return
     */
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public User findById(@RequestParam("id") Integer id) {
        return userService.getById(id);
    }


    /**
     * 修改密码
     */
    @RequestMapping(value = "/changePwd", method = RequestMethod.POST)
    @ResponseBody
    public boolean changePwd(@RequestBody User user,@RequestParam("newPassword") String newPassword ) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        user = userService.getUser(user);
        if(user == null){
            return false;
        }
        String password = newPassword;
        String salt = MD5Util.setSalt(5 + new Random().nextInt(5));
        password = MD5Util.setPassword(password, salt);
        user.setPassword(password);
        user.setSalt(salt);
        return userService.creatOrUpdate(user);
    }
}

